package com.epam.facebook_client;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.epam.facebook_client.api.API;
import com.epam.facebook_client.processor.PhotosProcessor;
import common.processor.IProcessor;

public class PostPhotoActivity extends BaseServiceActivity {

	private static final int RESULT_LOAD_IMAGE = 0;
	private static final String TAG = PostPhotoActivity.class.getSimpleName();
	private ImageView imageView;
	private Button openButton;
	private Button postButton;
	private Bitmap photoBitmap;
	private String idAlbum;
	private ProgressBar progressBar;
	private EditText postMessage;
	private String picturePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_photo);
		imageView = (ImageView) findViewById(R.id.imagePostPhoto);
		openButton = (Button) findViewById(R.id.openButton);
		postButton = (Button) findViewById(R.id.postButton);
		postMessage = (EditText) findViewById(R.id.postPhotoMessage);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		progressBar.setVisibility(View.GONE);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			idAlbum = extras.getString("id");
		} else {
			finish();
		}

		openButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent i = new Intent(Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, RESULT_LOAD_IMAGE);
			}
		});

		postButton.setEnabled(false);
		postButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Map<String, String> parameter = new HashMap<String, String>(4);
				parameter.put(PhotosProcessor.EXTRA_URL, API.getPostPhotoLink(idAlbum));
				parameter.put(PhotosProcessor.EXTRA_EXECUTE_METHOD, PhotosProcessor.METHOD_POST);
				parameter.put(PhotosProcessor.EXTRA_POST_SOURCE, picturePath);
				parameter.put(PhotosProcessor.EXTRA_POST_MESSAGE, postMessage.getText().toString());
				startServiceHelper(parameter, progressBar, PhotosProcessor.ACTION);
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			picturePath = cursor.getString(columnIndex);
			cursor.close();

			photoBitmap = BitmapFactory.decodeFile(picturePath);
			imageView.setImageBitmap(photoBitmap);
			postButton.setEnabled(true);
		} else {
			Log.i(TAG, "No result!!!");
		}

	}

	@Override
	public void onServiceResult(int requestId, Intent requestIntent, int resultCode, Bundle resultData) {
		progressBar.setVisibility(View.GONE);
		if (requestId == getRequestId()) {
			switch (resultCode) {
			case IProcessor.RESULT_OK:
				String response = resultData.getString(PhotosProcessor.EXTRA_RESPONSE);
				if (response.contains("post_id")) {
					Toast.makeText(getApplicationContext(), "Photo posted", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Photo not posted", Toast.LENGTH_LONG).show();
				}
				Log.i(TAG, "Response: " + response);
				break;
			case IProcessor.RESULT_ERROR:
				Exception e = (Exception) resultData.getSerializable(IProcessor.EXTRAS_ERROR);
				Log.e(TAG, "ERROR: " + e);
				Toast.makeText(this, "ERROR: " + e.getMessage(), Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	}
}
